{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 流式输出\n",
    "流式输出需要添加对应参数。其中，Python SDK需要添加stream=true"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"status_code\": 200, \"request_id\": \"0baa6bd1-9075-9757-a7c9-e501348adb57\", \"code\": \"\", \"message\": \"\", \"output\": {\"text\": null, \"finish_reason\": null, \"choices\": [{\"finish_reason\": \"null\", \"message\": {\"role\": \"assistant\", \"content\": \"材料\"}}]}, \"usage\": {\"input_tokens\": 7, \"output_tokens\": 1, \"total_tokens\": 8}}\n",
      "{\"status_code\": 200, \"request_id\": \"0baa6bd1-9075-9757-a7c9-e501348adb57\", \"code\": \"\", \"message\": \"\", \"output\": {\"text\": null, \"finish_reason\": null, \"choices\": [{\"finish_reason\": \"null\", \"message\": {\"role\": \"assistant\", \"content\": \"：牛腩1000克，西红柿\"}}]}, \"usage\": {\"input_tokens\": 7, \"output_tokens\": 11, \"total_tokens\": 18}}\n",
      "{\"status_code\": 200, \"request_id\": \"0baa6bd1-9075-9757-a7c9-e501348adb57\", \"code\": \"\", \"message\": \"\", \"output\": {\"text\": null, \"finish_reason\": null, \"choices\": [{\"finish_reason\": \"null\", \"message\": {\"role\": \"assistant\", \"content\": \"2个，大葱1根，姜3\"}}]}, \"usage\": {\"input_tokens\": 7, \"output_tokens\": 21, \"total_tokens\": 28}}\n",
      "{\"status_code\": 200, \"request_id\": \"0baa6bd1-9075-9757-a7c9-e501348adb57\", \"code\": \"\", \"message\": \"\", \"output\": {\"text\": null, \"finish_reason\": null, \"choices\": [{\"finish_reason\": \"null\", \"message\": {\"role\": \"assistant\", \"content\": \"片，蒜瓣6粒，八角1颗\"}}]}, \"usage\": {\"input_tokens\": 7, \"output_tokens\": 32, \"total_tokens\": 39}}\n",
      "{\"status_code\": 200, \"request_id\": \"0baa6bd1-9075-9757-a7c9-e501348adb57\", \"code\": \"\", \"message\": \"\", \"output\": {\"text\": null, \"finish_reason\": null, \"choices\": [{\"finish_reason\": \"null\", \"message\": {\"role\": \"assistant\", \"content\": \"，桂皮1小块，香叶2\"}}]}, \"usage\": {\"input_tokens\": 7, \"output_tokens\": 42, \"total_tokens\": 49}}\n",
      "{\"status_code\": 200, \"request_id\": \"0baa6bd1-9075-9757-a7c9-e501348adb57\", \"code\": \"\", \"message\": \"\", \"output\": {\"text\": null, \"finish_reason\": null, \"choices\": [{\"finish_reason\": \"null\", \"message\": {\"role\": \"assistant\", \"content\": \"片，干辣椒2只，生抽2勺\"}}]}, \"usage\": {\"input_tokens\": 7, \"output_tokens\": 53, \"total_tokens\": 60}}\n",
      "{\"status_code\": 200, \"request_id\": \"0baa6bd1-9075-9757-a7c9-e501348adb57\", \"code\": \"\", \"message\": \"\", \"output\": {\"text\": null, \"finish_reason\": null, \"choices\": [{\"finish_reason\": \"null\", \"message\": {\"role\": \"assistant\", \"content\": \"，老抽1勺，料酒2勺\"}}]}, \"usage\": {\"input_tokens\": 7, \"output_tokens\": 63, \"total_tokens\": 70}}\n",
      "{\"status_code\": 200, \"request_id\": \"0baa6bd1-9075-9757-a7c9-e501348adb57\", \"code\": \"\", \"message\": \"\", \"output\": {\"text\": null, \"finish_reason\": null, \"choices\": [{\"finish_reason\": \"null\", \"message\": {\"role\": \"assistant\", \"content\": \"，冰糖20克，盐适量\\n\\n做法\"}}]}, \"usage\": {\"input_tokens\": 7, \"output_tokens\": 74, \"total_tokens\": 81}}\n",
      "{\"status_code\": 200, \"request_id\": \"0baa6bd1-9075-9757-a7c9-e501348adb57\", \"code\": \"\", \"message\": \"\", \"output\": {\"text\": null, \"finish_reason\": null, \"choices\": [{\"finish_reason\": \"null\", \"message\": {\"role\": \"assistant\", \"content\": \"：\\n\\n1. 牛腩洗净切块，\"}}]}, \"usage\": {\"input_tokens\": 7, \"output_tokens\": 84, \"total_tokens\": 91}}\n",
      "{\"status_code\": 200, \"request_id\": \"0baa6bd1-9075-9757-a7c9-e501348adb57\", \"code\": \"\", \"message\": \"\", \"output\": {\"text\": null, \"finish_reason\": null, \"choices\": [{\"finish_reason\": \"null\", \"message\": {\"role\": \"assistant\", \"content\": \"放沸水中焯水去血沫捞出备用\"}}]}, \"usage\": {\"input_tokens\": 7, \"output_tokens\": 95, \"total_tokens\": 102}}\n",
      "{\"status_code\": 200, \"request_id\": \"0baa6bd1-9075-9757-a7c9-e501348adb57\", \"code\": \"\", \"message\": \"\", \"output\": {\"text\": null, \"finish_reason\": null, \"choices\": [{\"finish_reason\": \"null\", \"message\": {\"role\": \"assistant\", \"content\": \"。\\n2. 西红柿顶部划十字\"}}]}, \"usage\": {\"input_tokens\": 7, \"output_tokens\": 105, \"total_tokens\": 112}}\n",
      "{\"status_code\": 200, \"request_id\": \"0baa6bd1-9075-9757-a7c9-e501348adb57\", \"code\": \"\", \"message\": \"\", \"output\": {\"text\": null, \"finish_reason\": null, \"choices\": [{\"finish_reason\": \"null\", \"message\": {\"role\": \"assistant\", \"content\": \"刀口，放入沸水中烫一下去皮，\"}}]}, \"usage\": {\"input_tokens\": 7, \"output_tokens\": 116, \"total_tokens\": 123}}\n",
      "{\"status_code\": 200, \"request_id\": \"0baa6bd1-9075-9757-a7c9-e501348adb57\", \"code\": \"\", \"message\": \"\", \"output\": {\"text\": null, \"finish_reason\": null, \"choices\": [{\"finish_reason\": \"null\", \"message\": {\"role\": \"assistant\", \"content\": \"切滚刀块备用。\\n3. 大\"}}]}, \"usage\": {\"input_tokens\": 7, \"output_tokens\": 126, \"total_tokens\": 133}}\n",
      "{\"status_code\": 200, \"request_id\": \"0baa6bd1-9075-9757-a7c9-e501348adb57\", \"code\": \"\", \"message\": \"\", \"output\": {\"text\": null, \"finish_reason\": null, \"choices\": [{\"finish_reason\": \"null\", \"message\": {\"role\": \"assistant\", \"content\": \"葱切段、姜切片、蒜瓣剥\"}}]}, \"usage\": {\"input_tokens\": 7, \"output_tokens\": 137, \"total_tokens\": 144}}\n",
      "{\"status_code\": 200, \"request_id\": \"0baa6bd1-9075-9757-a7c9-e501348adb57\", \"code\": \"\", \"message\": \"\", \"output\": {\"text\": null, \"finish_reason\": null, \"choices\": [{\"finish_reason\": \"null\", \"message\": {\"role\": \"assistant\", \"content\": \"好。锅内油烧热，下入\"}}]}, \"usage\": {\"input_tokens\": 7, \"output_tokens\": 147, \"total_tokens\": 154}}\n",
      "{\"status_code\": 200, \"request_id\": \"0baa6bd1-9075-9757-a7c9-e501348adb57\", \"code\": \"\", \"message\": \"\", \"output\": {\"text\": null, \"finish_reason\": null, \"choices\": [{\"finish_reason\": \"null\", \"message\": {\"role\": \"assistant\", \"content\": \"大葱段、姜片、蒜瓣、八\"}}]}, \"usage\": {\"input_tokens\": 7, \"output_tokens\": 158, \"total_tokens\": 165}}\n",
      "{\"status_code\": 200, \"request_id\": \"0baa6bd1-9075-9757-a7c9-e501348adb57\", \"code\": \"\", \"message\": \"\", \"output\": {\"text\": null, \"finish_reason\": null, \"choices\": [{\"finish_reason\": \"null\", \"message\": {\"role\": \"assistant\", \"content\": \"角、桂皮、香叶和干辣椒\"}}]}, \"usage\": {\"input_tokens\": 7, \"output_tokens\": 168, \"total_tokens\": 175}}\n",
      "{\"status_code\": 200, \"request_id\": \"0baa6bd1-9075-9757-a7c9-e501348adb57\", \"code\": \"\", \"message\": \"\", \"output\": {\"text\": null, \"finish_reason\": null, \"choices\": [{\"finish_reason\": \"null\", \"message\": {\"role\": \"assistant\", \"content\": \"炒香。\\n4. 下入牛腩翻炒\"}}]}, \"usage\": {\"input_tokens\": 7, \"output_tokens\": 179, \"total_tokens\": 186}}\n",
      "{\"status_code\": 200, \"request_id\": \"0baa6bd1-9075-9757-a7c9-e501348adb57\", \"code\": \"\", \"message\": \"\", \"output\": {\"text\": null, \"finish_reason\": null, \"choices\": [{\"finish_reason\": \"null\", \"message\": {\"role\": \"assistant\", \"content\": \"均匀，烹入料酒炒匀后，\"}}]}, \"usage\": {\"input_tokens\": 7, \"output_tokens\": 189, \"total_tokens\": 196}}\n",
      "{\"status_code\": 200, \"request_id\": \"0baa6bd1-9075-9757-a7c9-e501348adb57\", \"code\": \"\", \"message\": \"\", \"output\": {\"text\": null, \"finish_reason\": null, \"choices\": [{\"finish_reason\": \"null\", \"message\": {\"role\": \"assistant\", \"content\": \"加入生抽和老抽炒至上色。\\n5\"}}]}, \"usage\": {\"input_tokens\": 7, \"output_tokens\": 200, \"total_tokens\": 207}}\n",
      "{\"status_code\": 200, \"request_id\": \"0baa6bd1-9075-9757-a7c9-e501348adb57\", \"code\": \"\", \"message\": \"\", \"output\": {\"text\": null, \"finish_reason\": null, \"choices\": [{\"finish_reason\": \"null\", \"message\": {\"role\": \"assistant\", \"content\": \". 加入足量热水（没过牛\"}}]}, \"usage\": {\"input_tokens\": 7, \"output_tokens\": 210, \"total_tokens\": 217}}\n",
      "{\"status_code\": 200, \"request_id\": \"0baa6bd1-9075-9757-a7c9-e501348adb57\", \"code\": \"\", \"message\": \"\", \"output\": {\"text\": null, \"finish_reason\": null, \"choices\": [{\"finish_reason\": \"null\", \"message\": {\"role\": \"assistant\", \"content\": \"腩），大火煮开后撇去浮沫，\"}}]}, \"usage\": {\"input_tokens\": 7, \"output_tokens\": 221, \"total_tokens\": 228}}\n",
      "{\"status_code\": 200, \"request_id\": \"0baa6bd1-9075-9757-a7c9-e501348adb57\", \"code\": \"\", \"message\": \"\", \"output\": {\"text\": null, \"finish_reason\": null, \"choices\": [{\"finish_reason\": \"null\", \"message\": {\"role\": \"assistant\", \"content\": \"转中小火慢慢炖煮至牛腩软\"}}]}, \"usage\": {\"input_tokens\": 7, \"output_tokens\": 231, \"total_tokens\": 238}}\n",
      "{\"status_code\": 200, \"request_id\": \"0baa6bd1-9075-9757-a7c9-e501348adb57\", \"code\": \"\", \"message\": \"\", \"output\": {\"text\": null, \"finish_reason\": null, \"choices\": [{\"finish_reason\": \"null\", \"message\": {\"role\": \"assistant\", \"content\": \"烂，约需1小时以上。\\n6.\"}}]}, \"usage\": {\"input_tokens\": 7, \"output_tokens\": 241, \"total_tokens\": 248}}\n",
      "{\"status_code\": 200, \"request_id\": \"0baa6bd1-9075-9757-a7c9-e501348adb57\", \"code\": \"\", \"message\": \"\", \"output\": {\"text\": null, \"finish_reason\": null, \"choices\": [{\"finish_reason\": \"null\", \"message\": {\"role\": \"assistant\", \"content\": \" 当牛腩炖至8成熟时，加入西红柿\"}}]}, \"usage\": {\"input_tokens\": 7, \"output_tokens\": 252, \"total_tokens\": 259}}\n",
      "{\"status_code\": 200, \"request_id\": \"0baa6bd1-9075-9757-a7c9-e501348adb57\", \"code\": \"\", \"message\": \"\", \"output\": {\"text\": null, \"finish_reason\": null, \"choices\": [{\"finish_reason\": \"null\", \"message\": {\"role\": \"assistant\", \"content\": \"块继续炖煮，炖煮至西红柿软\"}}]}, \"usage\": {\"input_tokens\": 7, \"output_tokens\": 262, \"total_tokens\": 269}}\n",
      "{\"status_code\": 200, \"request_id\": \"0baa6bd1-9075-9757-a7c9-e501348adb57\", \"code\": \"\", \"message\": \"\", \"output\": {\"text\": null, \"finish_reason\": null, \"choices\": [{\"finish_reason\": \"null\", \"message\": {\"role\": \"assistant\", \"content\": \"烂且汤汁浓稠即可，最后调入\"}}]}, \"usage\": {\"input_tokens\": 7, \"output_tokens\": 273, \"total_tokens\": 280}}\n",
      "{\"status_code\": 200, \"request_id\": \"0baa6bd1-9075-9757-a7c9-e501348adb57\", \"code\": \"\", \"message\": \"\", \"output\": {\"text\": null, \"finish_reason\": null, \"choices\": [{\"finish_reason\": \"null\", \"message\": {\"role\": \"assistant\", \"content\": \"适量的盐调味。\\n\\n西红柿炖牛腩就\"}}]}, \"usage\": {\"input_tokens\": 7, \"output_tokens\": 283, \"total_tokens\": 290}}\n",
      "{\"status_code\": 200, \"request_id\": \"0baa6bd1-9075-9757-a7c9-e501348adb57\", \"code\": \"\", \"message\": \"\", \"output\": {\"text\": null, \"finish_reason\": null, \"choices\": [{\"finish_reason\": \"null\", \"message\": {\"role\": \"assistant\", \"content\": \"做好了！\"}}]}, \"usage\": {\"input_tokens\": 7, \"output_tokens\": 286, \"total_tokens\": 293}}\n",
      "{\"status_code\": 200, \"request_id\": \"0baa6bd1-9075-9757-a7c9-e501348adb57\", \"code\": \"\", \"message\": \"\", \"output\": {\"text\": null, \"finish_reason\": null, \"choices\": [{\"finish_reason\": \"stop\", \"message\": {\"role\": \"assistant\", \"content\": \"\"}}]}, \"usage\": {\"input_tokens\": 7, \"output_tokens\": 286, \"total_tokens\": 293}}\n",
      "Full response:\n",
      "材料：牛腩1000克，西红柿2个，大葱1根，姜3片，蒜瓣6粒，八角1颗，桂皮1小块，香叶2片，干辣椒2只，生抽2勺，老抽1勺，料酒2勺，冰糖20克，盐适量\n",
      "\n",
      "做法：\n",
      "\n",
      "1. 牛腩洗净切块，放沸水中焯水去血沫捞出备用。\n",
      "2. 西红柿顶部划十字刀口，放入沸水中烫一下去皮，切滚刀块备用。\n",
      "3. 大葱切段、姜切片、蒜瓣剥好。锅内油烧热，下入大葱段、姜片、蒜瓣、八角、桂皮、香叶和干辣椒炒香。\n",
      "4. 下入牛腩翻炒均匀，烹入料酒炒匀后，加入生抽和老抽炒至上色。\n",
      "5. 加入足量热水（没过牛腩），大火煮开后撇去浮沫，转中小火慢慢炖煮至牛腩软烂，约需1小时以上。\n",
      "6. 当牛腩炖至8成熟时，加入西红柿块继续炖煮，炖煮至西红柿软烂且汤汁浓稠即可，最后调入适量的盐调味。\n",
      "\n",
      "西红柿炖牛腩就做好了！\n"
     ]
    }
   ],
   "source": [
    "from http import HTTPStatus\n",
    "from dashscope import Generation\n",
    "\n",
    "def call_with_stream():\n",
    "    messages = [\n",
    "        {'role': 'user', 'content': '如何做西红柿炖牛腩？'}]\n",
    "    responses = Generation.call(\n",
    "        Generation.Models.qwen_turbo,\n",
    "        api_key = \"sk-9d8f1914800e497f8717144e860f99bc\",\n",
    "        messages=messages,\n",
    "        result_format='message',  # set the result to be \"message\" format.\n",
    "        stream=True,\n",
    "        incremental_output=True  # get streaming output incrementally\n",
    "    )\n",
    "    full_content = ''  # with incrementally we need to merge output.\n",
    "    for response in responses:\n",
    "        if response.status_code == HTTPStatus.OK:\n",
    "            full_content += response.output.choices[0]['message']['content']\n",
    "            print(response)\n",
    "        else:\n",
    "            print('Request id: %s, Status code: %s, error code: %s, error message: %s' % (\n",
    "                response.request_id, response.status_code,\n",
    "                response.code, response.message\n",
    "            ))\n",
    "    print('Full response:\\n' + full_content)\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    call_with_stream()\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.9"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "eec67444d00d22f71b39bd6bbad710a72749a23d029a63a184fb88e8d46e38e1"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
